package dongshi.daddy.dongsrmutitidatasoucespringbootstarter.configuration;

import dongshi.daddy.dongsrmutitidatasoucespringbootstarter.datasource.MyCustomDatasource;
import dongshi.daddy.dongsrmutitidatasoucespringbootstarter.properties.CustomDatasourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@EnableConfigurationProperties(value = CustomDatasourceProperties.class)
@Configuration
public class MultiDatasourceBeanConfiguration {

    @Autowired
    private CustomDatasourceProperties customDatasourceProperties;

    // 设置使用的Datasource的bean,猜测会从容器中获取该自定义数据源对象
    @Bean
    public DataSource dataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.type(MyCustomDatasource.class);
        return dataSourceBuilder.build();
    }

}
